import tkinter as tk
from tkinter import messagebox
import mysql.connector

# 数据库连接配置
db_config = {
    'user': 'root',
    'password': 'root',
    'host': 'localhost',
    'database': 'mysql',
    'raise_on_warnings': True
}

# 检查用户名和密码是否正确
def check_credentials(username, password):
    try:
        # 创建数据库连接
        cnx = mysql.connector.connect(**db_config)
        cursor = cnx.cursor()

        # 查询数据库以验证凭据
        query = ("SELECT * FROM login WHERE count = %s AND password = %s")
        cursor.execute(query, (username, password))

        # 检查是否有匹配的行
        if cursor.fetchone() is not None:
            return True
        else:
            return False
    except mysql.connector.Error as err:
        print(f"Error: '{err}'")
        return False
    finally:
        # 关闭数据库连接
        if cnx.is_connected():
            cursor.close()
            cnx.close()

# 登录按钮点击事件处理函数
def on_login_click():
    username = entry_username.get()
    password = entry_password.get()
    if check_credentials(username, password):
        messagebox.showinfo("登录成功", "登录成功，欢迎！")
        root.destroy()  # 关闭登录窗口
        open_feature_window()  # 打开功能选择界面窗口
    else:
        messagebox.showerror("登录失败", "用户名或密码错误，请重试。")

# 打开功能选择界面
def open_feature_window():
    # 创建功能选择界面窗口
    feature_window = tk.Tk()
    feature_window.title("功能选择")

    # 设置窗口大小
    feature_window.geometry("450x350")  # 设置窗口的宽度和高度

    # 计算窗口居中显示的位置
    screen_width = feature_window.winfo_screenwidth()
    screen_height = feature_window.winfo_screenheight()
    x_cordinate = int((screen_width / 2) - (450 / 2))
    y_cordinate = int((screen_height / 2) - (350 / 2))
    feature_window.geometry("+{}+{}".format(x_cordinate, y_cordinate))

    # 使用LabelFrame来组织功能选项
    label_frame = tk.LabelFrame(feature_window, text="请选择功能")
    label_frame.pack(pady=20, padx=20, fill="both", expand=True)

    # 创建功能按钮并添加到LabelFrame中
    def on_supplier_management():
        messagebox.showinfo("功能选择", "供应商管理被选中")

    def on_employee_management():
        messagebox.showinfo("功能选择", "员工管理被选中")

    def on_product_management():
        messagebox.showinfo("功能选择", "商品管理被选中")

    def on_purchase_management():
        messagebox.showinfo("功能选择", "采购管理被选中")

    # 功能选择界面中的按钮
    btn_supplier_management = tk.Button(label_frame, text="供应商管理", command=on_supplier_management,font=('Arial', 15))
    btn_supplier_management.pack(pady=15)

    btn_employee_management = tk.Button(label_frame, text="员工管理", command=on_employee_management,font=('Arial', 15))
    btn_employee_management.pack(pady=15)

    btn_product_management = tk.Button(label_frame, text="商品管理", command=on_product_management, font=('Arial', 15))
    btn_product_management.pack(pady=15)

    btn_purchase_management = tk.Button(label_frame, text="采购管理", command=on_purchase_management,font=('Arial', 15))
    btn_purchase_management.pack(pady=15)

    feature_window.mainloop()

# 创建登录窗口
root = tk.Tk()
root.title("用户登录")
root.geometry('400x250+500+250')
root.config(bg='white')
root.resizable(width=False, height=False)

frame = tk.Frame(root, bg='white')
frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

tk.Label(frame, text="用户名:", bg="white").grid(row=0, column=0, padx=5, pady=5)
entry_username = tk.Entry(frame, width=30)
entry_username.grid(row=0, column=1)

tk.Label(frame, text="密码:", bg="white").grid(row=1, column=0, padx=5, pady=5)
entry_password = tk.Entry(frame, show="*", width=30)
entry_password.grid(row=1, column=1)

login_button = tk.Button(frame, text="登录", width=15, height=2, command=on_login_click)
login_button.grid(row=2, column=0, columnspan=2, pady=20)

root.mainloop()